//
//  WHLeftImgButtonCell.m
//  WHPersonalData
//
//  Created by hyf on 2023/7/22.
//  Copyright © 2023 mac. All rights reserved.
//

static int WHLeftImgButtonCellHeight = 39;

#import "WHLeftImgButtonViewCell.h"
#import "SDAutoLayout.h"
#import "WHHeaderButtonModel.h"

@interface WHLeftImgButtonViewCell ()
@property(nonatomic,strong)UIView *mbgView;
@property(nonatomic,strong)UIView *mRounbgView;
@property(nonatomic,strong)UIImageView *micon;
@property(strong,nonatomic)UILabel *mNameLabel;
@property (strong, nonatomic) WHHeaderButtonModel*cateModel;
@end

@implementation WHLeftImgButtonViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setViewChilds];
    }
    return self;
}

-(void) setViewChilds{
    UIView *bgView =[[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    _mbgView = bgView;
    [self.contentView addSubview:_mbgView];
    
    UIView *bgRounView =[[UIView alloc] init];
    bgRounView.layer.cornerRadius = 5;
    _mRounbgView = bgRounView;
    [_mbgView addSubview:_mRounbgView];
    
    UIImageView *img = [[UIImageView alloc] init];
    img.contentMode = UIViewContentModeScaleAspectFit;
    _micon = img;
    [_mRounbgView addSubview:_micon];
    
    UILabel *name = [[UILabel alloc] init];
    name.textAlignment = NSTextAlignmentCenter;
    name.font = [UIFont systemFontOfSize:14];
    name.numberOfLines = 1;
    _mNameLabel = name;
    [_mRounbgView addSubview:_mNameLabel];
    
}

-(void) setViewChildsAttr{
    _mbgView
    .sd_layout
    .topSpaceToView(self.contentView, 0)
    .bottomSpaceToView(self.contentView, 0)
    .leftSpaceToView(self.contentView, 0)
    .rightSpaceToView(self.contentView, 0);
    
    _mRounbgView
    .sd_layout
    .heightIs(WHLeftImgButtonCellHeight)
    .leftSpaceToView(_mbgView, 5)
    .rightSpaceToView(_mbgView, 5)
    .centerYEqualToView(_mbgView);
    
    _micon
    .sd_layout
    .leftSpaceToView(_mRounbgView, 10)
    .centerYEqualToView(_mRounbgView)
    .heightIs(19)
    .widthIs(18);

    if(self.cateModel.noSelImg){
        _mNameLabel
        .sd_layout
        .leftSpaceToView(_micon, 5)
        .rightSpaceToView(_mRounbgView, 0)
        .heightIs(WHLeftImgButtonCellHeight);
    }else{
        _mNameLabel
        .sd_layout
        .leftSpaceToView(_mRounbgView, 0)
        .rightSpaceToView(_mRounbgView, 0)
        .heightIs(WHLeftImgButtonCellHeight);
    }
}

- (void)setCateModel:(WHHeaderButtonModel *)cateModel{
    _cateModel = cateModel;
    [self setViewChildsAttr];
    if(cateModel.isSel){
        if(cateModel.selImg){
            UIImage * myImage = [UIImage imageNamed:cateModel.selImg];
            self.micon.image = myImage;
            self.micon.hidden = NO;
        }else{
            self.micon.hidden = YES;
        }
        
        if(cateModel.selBgColor){
            self.mRounbgView.backgroundColor = cateModel.selBgColor;
        }else{
            self.mRounbgView.backgroundColor = RGBColor(74, 188, 132);
        }
        if(cateModel.selColor){
            self.mNameLabel.textColor = cateModel.selColor;
        }else{
            self.mNameLabel.textColor = RGBColor(255, 255, 255);
        }
    }else{
        if(cateModel.noSelImg){
            UIImage * myImage = [UIImage imageNamed:cateModel.noSelImg];
            self.micon.image = myImage;
            self.micon.hidden = NO;
        }else{
            self.micon.hidden = YES;
        }
        
        if(cateModel.noSelBgColor){
            self.mRounbgView.backgroundColor = cateModel.noSelBgColor;
        }else{
            self.mRounbgView.backgroundColor = RGBColor(237, 244, 247);
        }
        if(cateModel.noSelColor){
            self.mNameLabel.textColor = cateModel.noSelColor;
        }else{
            self.mNameLabel.textColor = RGBColor(99, 107, 114);
        }
    }

    if(cateModel.font)
    {
        self.mNameLabel.font = cateModel.font;
    }else{
        self.mNameLabel.font = [UIFont systemFontOfSize:14];
    }
    
    self.mNameLabel.text = cateModel.title;
}

@end
